package com.csun.dsms.config;

import com.csun.dsms.model.huang.HgPushCycleData;
import com.csun.dsms.service.HgPushCycleService;
import com.csun.dsms.task.PeriodQuartz;
import com.csun.dsms.utils.QuartzManagerUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
public class QuartzConfig {

    @Resource
    private QuartzManagerUtil quartzManager;

    @Resource
    private HgPushCycleService hgPushCycleService;


    /**
     * 添加黄冈经济运行调度平台定时任务
     *
     * @return
     */
    @Bean
    public PeriodQuartz addPeriodQuartz() {
        Map<String, Object> map = new JobDataMap();
        Long groupId = 0L;
        //查询机构的周期设置
        List<HgPushCycleData> list = hgPushCycleService.selectAll();
        try {
            if (null != list && list.size() > 0) {
                for (HgPushCycleData cycleData : list) {
                    if (cycleData.getStatus()!=null && cycleData.getStatus() == 2){
                    }else{
                        map.put("ihgId", cycleData.getIhgId());
                        map.put("phone",cycleData.getPhone());
                        map.put("department",cycleData.getDepartment());
                        map.put("userName",cycleData.getUserName());
                        map.put("type",cycleData.getType());
                        map.put("catalog",cycleData.getCatalog());
                        map.put("title",cycleData.getTitle());
                        map.put("remark",cycleData.getRemark());
                        String JOB_GROUP_NAME = "JOB_HG_PUSH_"+cycleData.getId();
                        String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_NAME_PERIOD_"+cycleData.getId();
                        quartzManager.addJob(groupId.toString(), JOB_GROUP_NAME,
                                groupId.toString(), TRIGGER_GROUP_NAME,
                                PeriodQuartz.class, cycleData.getGenerateConfig(), map);
                    }
                }
            }
        } catch (Exception e) {
            log.info("创建周期账单定时任务失败,机构ID:" + groupId + "的机构创建定时任务失败");
        }

        return new PeriodQuartz();
    }

}
